package com.example.Bean;

import com.example.Annotation.AspectAnnotation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
@Data
@Slf4j
public class Peson implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean, BeanPostProcessor


{

    private String name;
    private Integer age;
    private String email;

   public Peson() {

        log.info("我的构造方法");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.info("初始化每一个Bean的时候都会执行postProcessBeforeInitialization--------{}", beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info("初始化每一个Bean的时候都会执行postProcessAfterInitialization--------{}", beanName);
        return bean;
    }

    @PostConstruct
    //初始化方法的注解方式  等同与init-method=init
    public void init() {
        log.info("@PostConstruct 调用初始化方法....");
    }

    @PreDestroy
    //销毁方法的注解方式  等同于destory-method=destory
    //@AspectAnnotation  此时注解在此是没有作用的无法进行aop拦截的
    public void destory() {

        log.info("PreDestroy注解调用销毁化方法....");

    }

    @AspectAnnotation
    public void print() {
        log.info("Print");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("实现ApplicationContextAware接口我拿到了ApplicationContext对象");
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("实现BeanFactoryAware接口我拿到了BeanFactory对象");

    }

    public void setBeanName(String s) {
        log.info("实现BeanNameAware接口我可以修改Bean对象的名字" + s);

    }

    public void afterPropertiesSet() throws Exception {
        log.info("实现InitializingBean接口我在装载Bean后可以处理一些事情");
    }

    @Override
    public void destroy() throws Exception {
        log.info("实现DisposableBean接口我在销毁的时候可以干这个事情");

    }
}
